<?php
/**
 * Created by PhpStorm
 * user: MingLiang
 * Date：2021/9/2
 * Time: 6:07 下午
 */


namespace app\admin\service;


class DateService
{

    public static function getDataChange($data,$array=false)
    {
        $arr = [];
        for($i=0, $iMax = count($data); $i< $iMax; $i++){
            for($j=0, $jMax = count($data[$i]); $j< $jMax; $j++){
                $array ? $arr[$array[$j]][$i] = $data[$i][$j] : $arr[$j][$i] = $data[$i][$j];
            }
        }
        return $arr;
    }


    public static function getDate($date)
    {
        $firstday = date('Y-m-01', strtotime($date));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        return array('first_day' => $firstday,'last_day' => $lastday);
    }

    public static function getlastMonthDays($date,$day=true){
         $timestamp=strtotime($date);
         if($day){
             $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
             $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
         }else{
             $firstday=date('Y-m',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1)));
             $lastday=date('Y-m',$timestamp);
         }

         return array($firstday,$lastday);
     }



    public static function getYear(){
        for($i=1;$i<=12;$i++){
            $data = date('Y-'.$i.'-01');
            $arr[] = self::getDate($data);
        }
        return $arr;
    }

    public static function actionQuarter(){
        $time = time();
        $data = [];
        $season = ceil((date('n', $time))/3);//当月是第几季度
        $season = $season*3-3+1; //季度的第一个月
        for ($i=$season;$i<$season+3;$i++){
           $f = date('Y-m-d', mktime(0, 0, 0,$i,1,date('Y')));
           $data[] = self::getDate($f);
        }
        return $data;
    }

    public static function actionYear($time){
        $date = date('Y-m-d',time());
        $data = [];
        for ($i=1;$i<=12;$i++){
            $f = date('Y-m-d', mktime(0, 0, 0,$i,1,$time));
            $data[] = self::getDate($f);
        }
        return $data;

    }

    private function getQuarter($y,$m,$month31,$quarterTimes=4){
        $quarterList = [];
        $year = $y;
        for($i=1;$i<=$quarterTimes;$i++){
            # 每个季度月份
            $quarter = [
                ['01','02','03'],
                ['04','05','06'],
                ['07','08','09'],
                [10,11,12]
            ];
            foreach ($quarter as $v){
                if(in_array($m,$v)){
                    $startDate = date($y.'-'.$v[0].'-01');
                    if(in_array((int)$v[2],$month31)){
                        $endDate = date($y.'-'.$v[2].'-31');
                    } else{
                        $endDate = date($y.'-'.$v[2].'-30');
                    }
                    # 不存在 2月份结束的 季度
                }
            }
            $quarterList[$i] = [$startDate,$endDate];
            if($m==1 && $y != $year){
                $m=12;
                $y = $year;
            } else {
                $m = date('m',strtotime($startDate)) - 3;
            }
            if($m == 1){
                $year-=1;
                continue;
            }
        }
        return $quarterList;
    }
}